// pages/employment/employment.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formItems: [
      { label: '求职岗位', field: 'position' },
      { label: '教育背景', field: 'education' },
      { label: '工作经历', field: 'work_experience' },
      { label: '项目经验', field: 'project_experience' },
      { label: '技能证书', field: 'certifications' }, // 若需区分技能和证书，可拆分为 skills 和 certifications
      { label: '荣誉奖励', field: 'awards' },
      { label: '其他信息', field: 'additional_info' }
    ],
    formData: {},
    resumeUrl: ''
  },
  onInput(e) {
    const field = e.currentTarget.dataset.field;
    this.setData({
      [`formData.${field}`]: e.detail.value
    });
  },
  generateResume() {
    const app = getApp();
    const userInfo = wx.getStorageSync('userInfo');
    const userId = userInfo.userid;
    
    if (!userId) {
      wx.showToast({ title: '用户未登录', icon: 'none' });
      return;
    }
    console.log(app.globalData.resumeData);
    wx.request({
      header: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      url: 'https://pszap.top/param/' + userId,
      method: 'POST',
      data: app.globalData.resumeData,
      success: (res) => {
        console.log(res)
        if (res.statusCode === 200) {
          wx.navigateTo({
            url: '/pages/employ-templates/employ-templates'
          });
        } else {
          wx.showToast({ title: res.data.msg || '保存失败', icon: 'none' });
        }
      },
      fail: () => {
        wx.showToast({ title: '网络错误', icon: 'none' });
      }
    });
  },
  // 获取字段标签
  getFieldLabel(field) {
    return this.data.formItems.find(item => item.field === field)?.label || '';
  },
  navigateToEdit(e) {
    const field = e.currentTarget.dataset.field;
    wx.navigateTo({
      url: `/pages/resume_edit/resume_edit?field=${field}`
    })
  },
  uploadImage() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      success: (res) => {
        this.setData({
          resumeUrl: res.tempFiles[0].tempFilePath
        });
        
        // 图片选择完成后再显示加载状态
        wx.showLoading({
          title: '上传中...',
          mask: true
        });
        
        setTimeout(() => {
          wx.hideLoading();
          wx.showToast({
            title: '上传失败',
            icon: 'none'
          });
        }, 1500);
      },
      fail: (err) => {
        wx.showToast({
          title: '上传失败',
          icon: 'none'
        });
        console.error('上传失败:', err);
      }
    });
}
  


})